package com.shell.provider;

import com.shell.bee.base.utils.StringUtil;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ContractTransferProvider {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private RestTemplate restTemplate;
	@Value("${governor.accreditation-api.serviceApp}")
	private String serviceApp;
	@Autowired
	private LoadBalancerClient loadBalancerClient;

	@RequestMapping({ "/contract/entUserSyncById" })
	public Map<String, Object> entUserSyncById(@RequestParam("cusId") String cusId) throws Exception {
		MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
		map.add("customerId", cusId);
		String apiUrl = "/contractApi/entUserSyncById";
		return post(apiUrl, map);
	}

	@RequestMapping({ "/contract/entUserSync" })
	Map<String, Object> entUserSync(@RequestParam("cusJson") String cusJson,
			@RequestParam("userJson") String userJson) {
		MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
		map.add("cusJson", cusJson);
		map.add("userJson", userJson);

		String apiUrl = "/contractApi/entUserSync";
		return post(apiUrl, map);
	}

	@RequestMapping({ "/contract/personalUserSyncById" })
	public Map<String, Object> personalUserSyncById(@RequestParam("userId") String userId) throws Exception {
		MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
		map.add("userId", userId);

		String apiUrl = "/contractApi/personalUserSyncById";
		return post(apiUrl, map);
	}

	@RequestMapping({ "/contract/personalUserSync" })
	public Map<String, Object> personalUserSync(@RequestParam("userJson") String userJson) throws Exception {
		MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
		map.add("userJson", userJson);

		String apiUrl = "/contractApi/personalUserSync";
		return post(apiUrl, map);
	}

	@RequestMapping({ "/contract/signerActiveSync" })
	public Map<String, Object> signerActiveSync(@RequestParam("cusId") String cusId,
			@RequestParam("userId") String userId) throws Exception {
		MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
		map.add("cusId", cusId);
		map.add("userId", userId);

		String apiUrl = "/contractApi/signerActiveSync";
		return post(apiUrl, map);
	}

	@RequestMapping({ "/contract/signerActiveSyncByUser" })
	Map<String, Object> signerActiveSyncByUser(@RequestParam("cusId") String cusId,
			@RequestParam("userJson") String userJson) {
		MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
		map.add("cusId", cusId);
		map.add("userJson", userJson);

		String apiUrl = "/contractApi/signerActiveSyncByUser";
		return post(apiUrl, map);
	}

	@RequestMapping({ "/contract/activeUser" })
	public Map<String, Object> activeUser(@RequestParam("userId") String userId) throws Exception {
		MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
		map.add("userId", userId);

		String apiUrl = "/contractApi/activeUser";
		return post(apiUrl, map);
	}

	@RequestMapping({ "/contract/freezeUser" })
	public Map<String, Object> freezeUser(@RequestParam("userId") String userId) throws Exception {
		MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
		map.add("userId", userId);

		String apiUrl = "/contractApi/freezeUser";
		return post(apiUrl, map);
	}

	private Map<String, Object> post(String apiUrl, MultiValueMap<String, Object> map) {
		if (StringUtil.empty(this.serviceApp)) {
			this.logger.info("没有配置服务");
			return null;
		}
		Map<String, Object> data = new HashMap<String, Object>();
		ServiceInstance serviceInstance = null;

		serviceInstance = this.loadBalancerClient.choose(this.serviceApp);
		if (serviceInstance == null) {
			throw new RuntimeException("服务名配置不正确");
		}
		String url = serviceInstance.getUri().toString() + "/" + this.serviceApp + apiUrl;
		this.logger.info("请求地址--------->" + url);

		Map<String, Object> responseData = (Map<String, Object>) this.restTemplate.postForObject(url, map, Map.class,
				new Object[0]);
		if (null != responseData) {
			data.putAll(responseData);
		}
		return data;
	}
}